//
//  ChooseImagesView.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/21.
//  Copyright © 2017年 maopu. All rights reserved.
//

import UIKit



class ChooseImagesView: UIView , UICollectionViewDataSource , UICollectionViewDelegate{
    fileprivate var collectionView:UICollectionView!
    fileprivate var imageArray : Array<UIImage>!
    fileprivate var cellWidth = Util.shared.getImgWidthForCollectionView(itemSpacing: 0, maxWidth: AppWidth, rowCount: 4)

    
    init(scrollDirection:UICollectionViewScrollDirection , frame: CGRect , cellWidth:CGFloat) {
        super.init(frame: frame)
        self.imageArray = Array<UIImage>()
        self.imageArray.removeAll()
        self.cellWidth = cellWidth
        self.initView(scrollDirection)
    }
    
    init(scrollDirection:UICollectionViewScrollDirection , frame: CGRect) {
        super.init(frame: frame)
        self.imageArray = Array<UIImage>()
        self.imageArray.removeAll()
        self.initView(scrollDirection)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initView(_ scrollDirection:UICollectionViewScrollDirection){
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: cellWidth, height: cellWidth)
        layout.scrollDirection = scrollDirection
        
        self.collectionView = UICollectionView.init(frame: self.frame, collectionViewLayout: layout)
        self.collectionView.backgroundColor = UIColor.clear
        self.collectionView.register(ChooseImagesItemCell.self, forCellWithReuseIdentifier: "cell")
        self.collectionView.showsHorizontalScrollIndicator = false
        self.collectionView.alwaysBounceHorizontal = true
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.addSubview(self.collectionView)
        
        self.frame = CGRect.init(x: self.frame.minX, y: self.frame.minY, width: self.frame.size.width, height: cellWidth)
        self.collectionView.frame = CGRect.init(x: 0, y: 0, width: self.frame.size.width, height: cellWidth)
        
//        self.refreshCellData()
    }
    
    func refreshCellData() -> Void {
        
        self.imageArray.append(UIImage(named: "video_thumb")!)
        self.imageArray.append(UIImage(named: "video_thumb")!)
        self.imageArray.append(UIImage(named: "video_thumb")!)
        self.imageArray.append(UIImage(named: "video_thumb")!)
        self.imageArray.append(UIImage(named: "video_thumb")!)
        self.collectionView.reloadData()
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if self.imageArray.count <= 0{
            return 0
        }
        return self.imageArray.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: ChooseImagesItemCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ChooseImagesItemCell
        if indexPath.row == self.imageArray.count{
            cell.refreshCell(UIImage(named: "addNewPicker")!, hiddenCloseBtn: true)
        }else{
            cell.refreshCell(self.imageArray[indexPath.row], hiddenCloseBtn: false)
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
        return CGSize(width: self.cellWidth, height: self.cellWidth)
    }

}

class ChooseImagesItemCell: UICollectionViewCell{
    var contentBtn : UIButton!
    var closeBtn : UIButton!
    var typeImg:UIImageView!
    
    override init(frame: CGRect){
        super.init(frame: frame)
        initializeView()
    }
    
    func initializeView() -> Void{
        self.backgroundColor = UIColor.white
        
        self.contentBtn = UIButton.init(type: .custom)
        self.contentBtn.frame = CGRect(x: 0, y: 5, width: self.frame.size.width - 5, height: self.frame.size.height - 5)
        self.contentBtn.backgroundColor = UIColor.clear
        self.contentBtn.adjustsImageWhenHighlighted = false
        self.contentBtn.imageView?.contentMode = .scaleAspectFill
        self.contentView.addSubview(self.contentBtn)
        
        self.closeBtn = UIButton.init(type: .custom)
        self.closeBtn.frame = CGRect(x: self.frame.size.width - 23, y: 0, width: 22, height: 22)
        self.closeBtn.backgroundColor = UIColor.clear
        self.closeBtn.adjustsImageWhenHighlighted = false
        self.closeBtn.imageView?.contentMode = .scaleAspectFill
        self.closeBtn.setImage(UIImage.init(named: "deleteImg_ico"), for: .normal)
        self.contentView.addSubview(self.closeBtn)
        
    }
    
    
    
    //刷新界面
    func refreshCell(_ imageObj: UIImage, hiddenCloseBtn:Bool , type:Int = 0) -> Void{
        self.contentBtn.setImage(imageObj, for:.normal)
        self.closeBtn.isHidden = hiddenCloseBtn
        
    }
    
    required init?(coder aDecoder: NSCoder){
        fatalError("init(coder:) has not been implemented")
    }
}
